package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.UsuarioBean;


/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		procesarRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		procesarRequest(request, response);
	}
	
	private void procesarRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		ArrayList<UsuarioBean> alUsuarios = new ArrayList<UsuarioBean>();
		alUsuarios = consultarUsuarios();
		String pagina = "/index.jsp";
		if(alUsuarios.size()>0){
			request.setAttribute("usuarios", alUsuarios);
			request.setAttribute("resultado", "Usuarios encontrados: " + alUsuarios.size());
		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(response.encodeURL(pagina));
		dispatcher.forward(request, response);

	}
	
	private ArrayList<UsuarioBean> consultarUsuarios() {
		ArrayList<UsuarioBean> listaUsuarios = new ArrayList<UsuarioBean>();

		Connection conn = conectarBD();
		PreparedStatement ps = null;
		ResultSet res = null;
		
		try {
			ps = conn.prepareStatement("Select * from usuarios");
			res = ps.executeQuery();
			
			while(res.next()){
				UsuarioBean ub = new UsuarioBean(res.getString("idusuario"), res.getString("clave"));
				listaUsuarios.add(ub);
				System.out.println("usuario: " + res.getString(1));
				System.out.println("clave: " + res.getString(2));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				res.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		

		return listaUsuarios;
	}

	private Connection conectarBD(){
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://" + "localhost" + ":" + "3306" + "/" + "test";
		Connection conn = null;
		
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, "root", "");
			
			return conn;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}

}
